<?php
class StockistsController extends AppController {
	var $name = 'Stockists';

    public function admin_listData ($type=null) {
        $rs = $this->Stockist->listing($this->params['form']);

        $json = ($rs) ? $rs : array(
            'page'  => 1,
            'total' => 0,
            'rows'  => array()
        );

        $this->response($json);
    } // admin_listData

    public function admin_update_status () {
        $json = array('status'=>1);
        $form = $this->params['form'];

        if ($form['task'] == 'delete') {
            $status = 'D';
            $action = 'delete';
        } else {
            $json = array(
                'status' => false,
                'error'  => 'Process failure, missing required data.'
            );
            $this->response($json);
            return;
        }

        if (empty($form['id'])) {
            $json = array(
                'status' => false,
                'error'  => 'Couldn\'t '.$action.' stockist, missing required record Id.'
            );
            $this->response($json);
            return;
        }

        $ids = explode(',', $form['id']);

        $rs = $this->Stockist->updateAll(
            array('Stockist.status' => '"'.$status.'"'),
            array('Stockist.id' => $ids)
        );

        if ($rs) {
            $json = array('status' => true);
        } else {
            $json = array(
                'status' => false,
                'error'  => 'Fail to '.$action.' stockist at this time.'
            );
        }

        $this->response($json);
    } // admin_update_status

    public function admin_update () {
        $this->data['Stockist'] = $this->params['form'];

        if ($this->data) {
            $valid = true;

            foreach ($this->data['Stockist'] as $fieldname=>$value) {
                if (empty($value) && $value != '0') {
                    $this->data['Stockist'][$fieldname] = null;
                }
            }

            $this->Stockist->set($this->data);

            if ($this->Stockist->validates() && $valid) { // data validated!
                if ($this->Stockist->save($this->data)) {
                    $mesg = empty($this->params['form']['id']) ? __('New stockist has been created successfully!',true) : __('Stockist has been updated successfully!',true);
                    $json = array(
                        'status' => true,
                        'mesg'   => $mesg
                    );
                } else {
                    $mesg = empty($this->params['form']['id']) ? __('Fail insert record to database at this time, please try again.',true) : __('Fail update record to database at this time, please try again.',true);
                    $err  = ($this->error) ? $this->error : $mesg;
                    $json = array(
                        'status' => false,
                        'mesg'   => $err
                    );
                }
            } else { // invalid data found
                $json = array(
                    'status' => false,
                    'mesg'   => __('Please enter the requested details in the fields pointed out below and try again!', true)
                );
                foreach ($this->Stockist->invalidFields() as $k=>$v) {
                    $json['error'][] = array('id'=>$k, 'field'=>$k, 'code'=>$v);
                }
            }
        } else {
            $json = array(
                'status' => false,
                'mesg'   => __('no data posted', true)
            );
        }

        $this->response($json);
    } // admin_update

    public function admin_get ($id) {
        $this->data = $this->Stockist->findById($id);

        if (empty($this->data['Stockist'])) {
            $json = array(
                'status' => false,
                'mesg'   => __('No data found from database', true)
            );
        }

        $data = array();
        $i    = 0;
        foreach ($this->data['Stockist'] as $f=>$v) {
            $data[$i] = array(
                'f' => $f,
                'v' => $v
            );

            if ($f == 'date_join' && $v) {
                $i++;
                $data[$i] = array(
                    'f' => 'show-date',
                    'v' => $this->_dateFormat($v)
                );
            }

            $i++;
        }

        $json = array(
            'status' => true,
            'data'   => $data
        );

        $this->response($json);
    } // admin_get

    public function admin_index () {
        $this->loadModel('Country');
        $data = $this->Country->find('list');

        $this->stylesheet  = am($this->stylesheet, 'flexigrid/flexigrid', 'autocomplete', 'jscal2/jscal2', 'popup');
        $this->javascripts = am($this->javascripts, 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', 'autocomplete.min', 'jscal2', 'flexigrid.min', 'admin_stockist');
        $this->pageTitle   = __('Stockist Management', true);

        $this->set('pageTitle', $this->pageTitle);
        $this->set('countries', $data);
        $this->render('admin_manage');
    } // admin_index


    private function _dateFormat ($date) {
        $time = strtotime($date);
        $day  = date('D', $time + CLIENT_TIMEZONE_OFFSET);
        $date = date(', j F Y', $time + CLIENT_TIMEZONE_OFFSET);
        $date = substr($day, 0, 2).$date;
        return $date;
    } // _dateFormat
} // StockistsController
?>